//
//  LoginViewController.m
//  UADE-Mobile
//
//  Created by Marcos Damian Pianelli on 9/19/11.
//  Copyright (c) 2011 DG. All rights reserved.
//

#import "LoginViewController.h"
#import "AppDelegate.h"

@implementation LoginViewController
@synthesize userField,passwordField, loginButton;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}



- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"1");
    [userField becomeFirstResponder];
    NSLog(@"2");

    
}

#pragma mark - View lifecycle

// @"bvenning" @"password"

-(IBAction)doLogin:(id)sender{
    if( ![userField.text isEqualToString:@""] && ![passwordField.text isEqualToString:@""]){
        StudentModel * model = [StudentModel SharedInstance];
        [loginButton setEnabled:FALSE];
        [loginButton setTitle:@"Procesando..." forState:0];
        [model doLogin:self user:userField.text password:passwordField.text];
    }else{
        TTALERTMSG( NSLocalizedString(@"Login",@""), NSLocalizedString(@"You should complete username and password fields before proceding with login.", @""));
    }
    
}


#pragma mark - StudentLoginDelegate

-(void)didLogin:(id)model{
    [loginButton setEnabled:TRUE];
   
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"kDidLoginNotification" object:nil];
    
    [self dismissModalViewControllerAnimated:TRUE];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate didLogin];
}
-(void)didFailLogin:(id)model{
    [loginButton setEnabled:TRUE];
    [loginButton setTitle:@"Login" forState:0];

    TTALERTMSG( NSLocalizedString(@"Login",@""), NSLocalizedString(@"There was a problem with the user or password", @""));

}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
